/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.test.dependency;

import com.genia.toolbox.persistence.criteria.Criteria;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency;
import com.genia.toolbox.persistence.test.bean.PersistentObjectB;
import com.genia.toolbox.persistence.test.bean.PersistentObjectC;

/**
 * Dependency management for class <code>PersistentObjectB</code>.
 */
public class PersistentObjectCDependency
    extends AbstractPersistableDependency<PersistentObjectC>
{

  /**
   * handle the dependencies before deleting an object.
   * 
   * @param object
   *          the object that will be deleted
   * @throws PersistenceException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onDelete(java.lang.Object)
   */
  @Override
  public void onDelete(final PersistentObjectC object)
      throws PersistenceException
  {
    final Criteria<PersistentObjectB> criteria = getPersistenceManager().getCriteria(PersistentObjectB.class);
    criteria.addRestriction(isIn(getConstantField(object), elements(getPropertyField(PersistentObjectB.NAME_VAR_PERSISTENT_OBJECT_C))));
    for (final PersistentObjectB pob : getPersistenceManager().simpleFind(criteria)) {
      pob.getVarPersistentObjectC().remove(object);
      getPersistenceManager().update(pob);
    }
  }



  /**
   * Constructor.
   */
  protected PersistentObjectCDependency()
  {
    super(PersistentObjectC.class);
  }

}
